﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DreamBuilder.Core;
using DreamBuilder.Common;
using DreamBuilder.Core.Container;
using ePlanet.ArchiveMange.Models;
using ePlanet.ArchiveMange.Services;
using System.Text;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.Models;
using ePlanet.SingleWell.DataAccess;

namespace Web.HttpHandles
{
    /// <summary>
    /// WellCatalogManageHandler 的摘要说明
    /// </summary>
    public class WellCatalogManageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string json = "[]";
            //获取文档类型
            //if (context.Request.QueryString["archivemenu"] != null)
            //{
                json = GetWellCatalogData();
            //}
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public string GetWellCatalogData()
        {
            //IWellCatalogService IArchiveMenu = UContainer.Resolve<IWellCatalogService>();
            WellCatalogDAL wellCatalogDAL = new WellCatalogDAL();
            WellCatalog.WELLCATALOGDataTable dtWellCatalog =
                wellCatalogDAL.GetAllWellCatalog();
            //dtWellCatalog.Rows.Add("0", "单井编目", "", "-1");
            string jsonFormat = "{{\"id\": \"{0}\", \"text\": \"{1}\",\"pid\": \"{2}\", \"iconCls\": \"{3}\",  \"state\": \"{4}\",\"attributes\":{{\"catelogSort\": \"{5}\",\"isWellCode\":\"{6}\"}}}},";
            StringBuilder resultStr = new StringBuilder("[");
            string iconCss = string.Empty;
            string ParentID = string.Empty;
            string isWellCode = string.Empty;
            foreach (WellCatalog.WELLCATALOGRow row in dtWellCatalog.Rows)
            {
                if (string.IsNullOrEmpty(row.PARENTID.ToString()))
                {
                    ParentID = "0";
                }
                else
                {
                    ParentID = row.PARENTID.ToString();
                }
                iconCss = ParentID == "-1" ? "icon-type" : "icon-folder";
                isWellCode = row.IsISWELLCODENull() ? "" : string.Concat(row.ISWELLCODE);
                resultStr.Append(string.Format(jsonFormat, row.WELLCATALOGID, row.CATALOGDESCRIPT, row.PARENTID, iconCss, "open", row.CATELOGSORT, isWellCode));
            }
            if (resultStr.Length > 1)
            {
                resultStr = resultStr.Remove(resultStr.Length - 1, 1);
            }
            resultStr.Append("]");
            return resultStr.ToString();
        }
    }
}